package cnic.ai.nacos.controller

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
import org.springframework.cloud.client.loadbalancer.LoadBalanced
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.client.RestTemplate
import org.springframework.web.client.getForObject
import javax.annotation.Resource

@RequestMapping("echo")
@Configuration
@RestController
public open class SayConsumerConroller {

    @Resource
    private val restTemplate: RestTemplate? = null

    @Value(value="\${provider.url}")
    lateinit var providerUrl: String

    @GetMapping("/{str}")
    fun echo(@PathVariable str: String): String? {
        var baseurl = providerUrl
        println("==========================consumer==========================")
        println("providerUrl=$baseurl")
        var url: String = "http://${baseurl}/say/${str}"
        try {
            var result = restTemplate?.getForObject(url, String::class.java)
            return result
        } catch (e: Exception) {
            println(e.stackTrace)
            return e.message
        }
    }
}